﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;

namespace RadarGlobal.MapClass
{
    class CustomPushpin : DependencyObject
    {
        MapLayer mapLayer = new MapLayer();
        // Chứa hình Pushpin cần pin ra
        MapOverlay shape = new MapOverlay();
        //Tiêu đề chữ của Pushpin
        TextBlock label = new TextBlock();
        //Tên pushpin để tiện việc xử lý
        public string Name { set; get; }

        Grid luoi = new Grid();

        ContextMenu menu = new ContextMenu();

        private void CreateTextBlock(string text)
        {
            label.Text = text;
            label.Foreground = new SolidColorBrush(Colors.Black);
            label.FontWeight = FontWeights.Bold;
            label.Width = 300;
            label.VerticalAlignment = VerticalAlignment.Center;
        }

       
        public void SetCircler(Map MapControl,int width=30, int height=30)
        {
            Ellipse circler = new Ellipse
            {
                Width = width,
                Height = height,
                Stroke=new SolidColorBrush(Colors.Black),
                StrokeThickness=3,
                Fill=new SolidColorBrush(Colors.Green)
            };
            circler.Margin = new Thickness(0, 0, 0, 0);
            circler.Margin = new Thickness(-width / 2, -height / 2, 0, 0);

            luoi.Children.Add(circler);

            shape.Content = luoi;

            mapLayer.Clear();

            mapLayer.Add(shape);

            MapControl.Layers.Add(mapLayer);
        }

        public void SetCircler2(Map MapControl,Color ColorForm, Color ColorTo,int width=30,int height=30)
        {
            Ellipse circler = new Ellipse
            {
                Width = width,
                Height = height,
                Fill = new SolidColorBrush(ColorForm)
            };
            circler.Margin = new Thickness(-(circler.Width / 2), -(circler.Height / 2), 0, 0);

            Storyboard sb = new Storyboard();

            ColorAnimation animatecolor = new ColorAnimation();

            animatecolor.From = ColorForm;
            animatecolor.To = ColorTo;
            animatecolor.BeginTime = TimeSpan.FromSeconds(0);
            animatecolor.Duration = new Duration(TimeSpan.FromSeconds(0.5));

            Storyboard.SetTarget(animatecolor, circler);
            Storyboard.SetTargetName(animatecolor, circler.Name);
            Storyboard.SetTargetProperty(animatecolor, new PropertyPath("(Ellipse.Fill).(SolidColorBrush.Color)"));

            sb.Children.Add(animatecolor);
            sb.RepeatBehavior = RepeatBehavior.Forever;
            sb.Begin();

            //CreateTextBlock("Tôi ở đây");

           // label.Margin = new Thickness(-(circler.Width / 2), -(height / 2 + 24), 0, 0);

            luoi.Children.Add(circler);
            //luoi.Children.Add(label);

           // luoi.Margin = new Thickness(-(circler.Width / 2), -(height / 2), 0, 0);

            shape.Content = luoi;

            mapLayer.Clear();

            mapLayer.Add(shape);

            MapControl.Layers.Add(mapLayer);
        }

        public void SetImagePushpin(Map MapControl)
        {
           Image pushpin = new Image();
           BitmapImage bi3 = new BitmapImage();
           bi3.UriSource = new Uri("/Icons/pushpin.png", UriKind.Relative);
           pushpin.Source = bi3;
           pushpin.Margin = new Thickness(-15, -46, 0, 0);
           luoi.Children.Clear();
           luoi.Children.Add(pushpin);
           shape.Content = luoi;

           mapLayer.Clear();

           mapLayer.Add(shape);
       
           MapControl.Layers.Add(mapLayer);
        }
        public void SetImagePushpin2(Map MapControl)
        {
            Image pushpin = new Image();
            BitmapImage bi3 = new BitmapImage();
            bi3.UriSource = new Uri("/Icons/pushpin2.png", UriKind.Relative);
            pushpin.Source = bi3;
            pushpin.Margin = new Thickness(-15, -46, 0, 0);
            luoi.Children.Clear();
            luoi.Children.Add(pushpin);
            shape.Content = luoi;

            mapLayer.Clear();

            mapLayer.Add(shape);

            MapControl.Layers.Add(mapLayer);
        }

        public void ShowPushpin(Map MapControl)
        {
            if (!MapControl.Layers.Contains(mapLayer))
            {
                MapControl.Layers.Add(mapLayer);
            }
        }
        public void RemovePushpin(Map MapControl)
        {
            if (MapControl.Layers.Contains(mapLayer))
            {
                MapControl.Layers.Remove(mapLayer);
            }
        }

        public void AddMenuContext(ContextMenu menu)
        {
            this.menu = menu;

            ContextMenuService.SetContextMenu((DependencyObject)label, this.menu);

            luoi.MouseLeftButtonDown += (s, args) =>
            {
                this.menu.IsOpen = true;
            };
        }

        public void UpdateLocation(GeoCoordinate center)
        {
            shape.GeoCoordinate = center;
        }
    }
}
